package com.sove.face.contant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class DatabaseInitializer implements CommandLineRunner {
 
    private final JdbcTemplate jdbcTemplate;
 
    @Autowired
    public DatabaseInitializer(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    @Override
    public void run(String... args) {
        // 检查数据库是否存在
        @SuppressWarnings("deprecation")
		boolean databaseExists = jdbcTemplate.queryForObject(
            "SELECT 1 FROM information_schema.schemata WHERE schema_name = ?",
            new Object[]{"sove_face"},
            Integer.class) == 1;
 
        if (!databaseExists) {
            // 创建数据库
            jdbcTemplate.execute("CREATE DATABASE sove_face CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
        }
    }
}